package be.djdb.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

import be.djdb.UnConstruct;

/**
* @author Lieven Roegiers
* @copyright 2011
* @from JAVA_mylibs
* @from http://code.google.com/p/javamylibs/
* @version java 1.5+
*/
public class Projectsettings extends Properties{
	private static final long serialVersionUID = 1L;
	private String filename;
	public  Projectsettings(String filename) {
       super();
       this.filename = filename+".MYsettings";
   }
   /*
    * You can detect if de settingsfile exist and 
    * you can use it as is you prog installed
    * */
   public boolean isInit(){
	  return new File(this.filename).exists();
   }
   public final void LoadSettings(){
       GZIPInputStream in = null;
       try{
           in = new GZIPInputStream(new FileInputStream(filename));
           loadFromXML(in);
           in.close();
       }catch (IOException ex){
           in = null;
       }
   }
   public String get(String k){
	   return super.getProperty(k);
   }
   public void set(String k,String v){
	    super.setProperty(k, v);
   }
   public void set(String k, JCheckBox v){
	   set(k,(v.isSelected()?"true":"false"));
   }
   public void setfsettings(Map<String,String> map){
       for (String k : map.keySet()) {
    	   setProperty(k, map.get(k));
       }
   }
   private boolean add(String filename){
	   try {
			load(getClass().getResourceAsStream(filename));
		} catch (IOException e) {
			return false;
		}
		return true;
   }
   @UnConstruct
   public Map<String,String>getm(String key){
	   return null;
   }
   public final boolean SaveSettings(){
      return SaveSettings(filename);
   }
   public final boolean SaveSettings(String filename){
       try{
       		GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(filename));
       		storeToXML(out, "Project settings", "UTF-8");
       		out.close();
       } catch (IOException ex1){
           return false;
       }
       return true;
   }
   public String toString(){
	 return super.toString();  
   }
   public DefaultListModel propertieslist(){
	   DefaultListModel listModel = new DefaultListModel();
	   return listModel;
   }
	public Hashtable<Object, Object> gethashtable() {
		return (Hashtable<Object, Object>) this;
	}
    public JPanel getEditor(){
    	return new ProjectSettingsEditor(this);
    }
}

